#!/usr/bin/env python
# encoding: utf-8
# Date: 2017-06-19 09:07:37
# file:views.py
# Email: wang521kang@outlook.com
# Author: 王康
from address.models import Address
from commodity.models import Commodity, CommodityImg
from order.models import Order, OrderCommodity
from shopping_cart.models import ShoppingCart
from utils.views import request_parms_check, get_json


@request_parms_check(cart_ids=('购物车id', 'r'))
def create_order(request, cart_ids):
    order = Order()
    order.user_id = request.user.id
    try:
        address = Address.objects.get(is_active=True, is_default=True, user_id=request.user.id)
    except Address.DoesNotExist:
        address = None
    if address:
        order.name = address.name
        order.phone = address.phone
        order.add_province = address.add_province
        order.add_city = address.add_city
        order.add_district = address.add_district
        order.add_detail = address.add_detail
    order.save()
    l = cart_ids.split(',')
    for cart_id in l:
        cart_id = int(cart_id)
        order_com = OrderCommodity()
        query_cart = ShoppingCart.objects.get(id=cart_id)
        order_com.commodity_id = query_cart.commodity_id
        order_com.price = query_cart.commodity.price
        order_com.num = query_cart.num
        order_com.order_id = order.id
        order_com.save()
        query_cart.is_active = False
        query_cart.save()
    res = {}
    res['id'] = order.id
    return get_json(True, '', res)


@request_parms_check(order_id=('订单id', 'r,int'), address_id=('地址id', 'r,int'))
def address_mod(request, address_id, order_id):
    order = Order.objects.get(id=order_id)
    address = Address.objects.get(id=address_id)
    order.name = address.name
    order.phone = address.phone
    order.add_province = address.add_province
    order.add_city = address.add_city
    order.add_district = address.add_district
    order.add_detail = address.add_detail
    order.save()
    return get_json(True, '')


def query_order(request):
    user_id = request.user.id
    query = Order.objects.filter(user_id=user_id).all()
    l = []
    for order in query:
        res = {}
        l2 = []
        res['id'] = order.id
        res['create_time'] = order.create_time.strftime('%Y-%m-%d %H:%M:%S')
        res['status'] = order.status
        ordercommodity = OrderCommodity.objects.filter(order_id=order.id).all()
        for item in ordercommodity:
            r = {}
            commodity = Commodity.objects.get(id=item.commodity_id)
            r['price'] = item.price
            r['num'] = item.num
            r['name'] = commodity.name
            r['preferential'] = commodity.preferential
            r['classification'] = commodity.commodity_classification.classification
            imgs = CommodityImg.objects.filter(is_active=True, commodity_id=commodity.id).all()
            if imgs:
                    r['img_url'] = imgs[0].img_url
            l2.append(r)
        res['commodity'] = l2
        l.append(res)
    return get_json(True, '', l)


@request_parms_check(order_id=('订单id', 'r,int'))
def get_order(request, order_id):
    order = Order.objects.get(id=order_id)
    res = {}
    l2 = []
    res['id'] = order.id
    res['create_time'] = order.create_time.strftime('%Y-%m-%d %HH:%MM:%SS')
    res['status'] = order.status
    res['name'] = order.name
    res['phone'] = order.phone
    res['add_province'] = order.add_province
    res['add_city'] = order.add_city
    res['add_district'] = order.add_district
    res['add_detail'] = order.add_detail
    ordercommodity = OrderCommodity.objects.filter(order_id=order.id).all()
    for item in ordercommodity:
        r = {}
        commodity = Commodity.objects.get(id=item.commodity_id)
        r['price'] = item.price
        r['num'] = item.num
        r['name'] = commodity.name
        r['preferential'] = commodity.preferential
        r['classification'] = commodity.commodity_classification.classification
        imgs = CommodityImg.objects.filter(is_active=True, commodity_id=commodity.id).all()
        if imgs:
            r['img_url'] = imgs[0].img_url
        l2.append(r)
    res['commodity'] = l2
    return get_json(True, '', res)


@request_parms_check(order_id=('订单id', 'r,int'))
def delete_order(request, order_id):
    order = Order.objects.get(id=order_id)
    order.is_active = False
    order.save()
    return get_json(True, '')


@request_parms_check(order_id=('订单id', 'r,int'))
def order_pay(request, order_id):
    order = Order.objects.get(id=order_id)
    order.status = order.Pay
    order.save()
    return get_json(True, '')


@request_parms_check(order_id=('订单id', 'r,int'))
def order_over(request, order_id):
    order = Order.objects.get(id=order_id)
    order.status = order.Over
    order.save()
    return get_json(True, '')
